/* 

Replication syntax BJPolS article
"Intergenerational social mobility, political socialization, and support for the left under post-industrial realignment"
Dr. Macarena Ares, University of Barcelona
Dr. Mathilde M. van Ditmars, University of Lucerne
March 2022 (Stata version 16)

File 4/4. Analyses
*/

*perform file creation etc. in designated dofiles
*use recoded data files to perform data analysis


global output "DEFINE"
global SHP "DEFINE" //  SHP data location
global BHPS "DEFINE" // BHPS data location
global GSOEP "DEFINE" // G-SOEP data location

********************************************************************************
*ANALYSIS UKHLS

use "${BHPS}/BHPS_US_workfile.dta"

numlabel, add
set scheme cleanplots

* Set the variables that order the panel
sort pidp wave
xtset pidp wave

* First: Definition of the analytical sample
* Generate the analytical sample based on age, nationality & key variables not missing, once we do this we select one observation per individual
gen sample=0
replace sample=1 if age>17 & jbstat!=7 & british_national==1 & class8_parent!=. & class8_r!=. & pref_left!=.
keep if sample==1

* Select one observation per respondent
* Generate an identifier for the first time an individual was included in the sample and one for the last time

by pidp, sort: gen nfirst= _n==1
count if nfirst
by pidp, sort: gen nlast= _n==_N
count if nlast
* we have 13,610 unique observations

bysort pidp: gen total= _N
bysort pidp: gen num_wave= _n
gen last_obs= total==num_wave

tab nfirst nlast
tab nfirst if nfirst==1 & nlast==1
* of which 2,172 are only observed once


*1. Table 1& 2: Cross tabulation: father's class x respondent' class
*using waves 2018-2009 
preserve 
keep if wave==26|wave==25|wave==24|wave==23|wave==22|wave==21|wave==20|wave==19|wave==18

tab simclass_parent2 simclass_r2, col, if last_obs==1 

restore 
//   unique observations

*2. Figure 1 and 2: LW support by parental and respondent's class, RE, excl. silent generation & time period 2009-2018

preserve 
keep if wave==26|wave==25|wave==24|wave==23|wave==22|wave==21|wave==20|wave==19|wave==18

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig1_table.xls", replace ctitle(UK) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
margins, dydx(simclass_r2) post coeflegend
est store uk_left_r_all

quietly xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_parent2) post coeflegend
est store uk_left_p_all

xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig2_table.xls", replace ctitle(UK) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
xttab generations2 if e(sample)
// between percentages: 48% Baby boomers, 44% Gen X, 8% Millennials

forvalues i=3/5 {
quietly xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_parent2) at(generations2==`i') post coeflegend
est store uk_left_p_gen_`i'
}

restore


*3. Figure 3 and 4:

*1, 3, 5, 6, 9, 11, 13 = of interest
*skip 5 and 11 for babyboomers

preserve 
keep if wave==26|wave==25|wave==24|wave==23|wave==22|wave==21|wave==20|wave==19|wave==18

xtreg left ib14.trans age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig3_table.xls", replace ctitle(UK) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
margins, dydx(trans) post coeflegend
est store uk_trans_all

xtreg left ib14.trans##ib3.generations2 age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig4_table.xls", replace ctitle(UK) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
margins, dydx(1.trans 3.trans 6.trans 9.trans 13.trans) at(generations2==3) post coeflegend
est store uk_trans_gen3

forvalues i=4/5 {
quietly xtreg left ib14.trans##ib3.generations2 age female parttime ib3.civsta_3, re
margins, dydx(1.trans 3.trans 5.trans 6.trans 9.trans 11.trans 13.trans) at(generations2==`i') post coeflegend
est store uk_trans_gen`i'
}

restore

**** extra analyses
*class-party alignments over time 


preserve 
keep if wave==1|wave==2|wave==3|wave==4|wave==5|wave==6|wave==7|wave==8|wave==9

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_r2) post coeflegend
est store uk_left_r_all_90s
restore

preserve 
keep if wave==10|wave==11|wave==12|wave==13|wave==14|wave==15|wave==16|wave==17

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_r2) post coeflegend
est store uk_left_r_all_00s
restore

preserve 
keep if wave==26|wave==25|wave==24|wave==23|wave==22|wave==21|wave==20|wave==19|wave==18


xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_r2) post coeflegend
est store uk_left_r_all_10s
restore


********************************************************************************
*ANALYSIS G-SOEP

use "${GSOEP}/GSOEP_v35_workfile.dta"

* Set the variables that order the panel
sort pid syear
xtset pid syear

* Define analytic sample: The analytic samples comprise all British respectively German, Swiss nationals aged over 17 syears (eligible to vote), who were at the time of the survey employed and not enrolled in fulltime education.

* Generate the analytical sample based on these traits & key variables not missing, once we do this we select one observation per individual
capture drop sample
gen sample=0
replace sample=1 if age>17 & plb0022_h<3 & educat_3!=0 & german==1 & class8_parent!=. & class8_r!=. & SPD!=.
keep if sample==1

* Select one observation per respondent
* Generate an identifier for the first time an individual was included in the sample and one for the last time
sort pid syear
bysort pid: gen total= _N
bysort pid: gen num_wave= _n
gen last_obs= total==num_wave

by pid, sort: gen nfirst= _n==1
count if nfirst
by pid, sort: gen nlast= _n==_N
count if nlast

tab nfirst nlast
*we have 25,599 respondents in the dataset
*there are 119,725 person-years

tab nfirst if nfirst==1 & nlast==1
* of which 6872 are only observed once

*1. Table 1& 2: Cross tabulation: father's class x respondent' class
*using waves 2018-2008
preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

tab simclass_parent2 simclass_r2, col, if last_obs==1 

restore

// 16,658  unique observations

*2. Figure 1 and 2: LW support by parental and respondent's class, RE, excl. silent generation & time period 2008-2018
preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989 , re
outreg2 using "${output}/Fig1_table.xls", append ctitle(DE) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o)) // between R-squared is 0.0159, overall is 0.0150 
margins, dydx(simclass_r2) post coeflegend
est store de_left_r_all

quietly xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989 , re
margins, dydx(simclass_parent2) post coeflegend
est store de_left_p_all

xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3 i.loc1989 , re
outreg2 using "${output}/Fig2_table.xls", append ctitle(DE) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
xttab generations2 if e(sample)
// between percentages: 39% Baby boomers, 47% Gen X, 14% Millennials

forvalues i=3/5 {
quietly xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3 i.loc1989 , re
margins, dydx(simclass_parent2) at(generations2==`i') post coeflegend
est store de_left_p_gen_`i'
}

restore




*3. Figure 3 and 4:

*1, 3, 5, 6, 9, 11, 13 = of interest
*skip 5 and 11 for babyboomers

preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

xtreg left ib14.trans age female parttime ib3.civsta_3 i.loc1989, re
outreg2 using "${output}/Fig3_table.xls", append ctitle(DE) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
margins, dydx(trans) post coeflegend
est store de_trans_all

xtreg left ib14.trans##ib3.generations2 age female parttime ib3.civsta_3 i.loc1989, re
outreg2 using "${output}/Fig4_table.xls", append ctitle(DE) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
margins, dydx(1.trans 3.trans 6.trans 9.trans 13.trans ) at(generations2==3) post coeflegend
est store de_trans_gen3

forvalues i=4/5 {
quietly xtreg left ib14.trans##ib3.generations2 age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(1.trans 3.trans 5.trans 6.trans 9.trans 11.trans 13.trans) at(generations2==`i') post coeflegend
est store de_trans_gen`i'
}

restore

****extra analyses

*** dependent variable incl. the greens

*Figure 1, 2 and Figure 3 models
preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

xtreg left_green ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(simclass_r2) post coeflegend
est store de_leftinclgreens_r_all

quietly xtreg left_green ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(simclass_parent2) post coeflegend
est store de_leftinclgreens_p_all

forvalues i=3/5 {
quietly xtreg left_green ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(simclass_parent2) at(generations2==`i') post coeflegend
est store de_leftinclgreens_p_gen_`i'
}

xtreg left_green ib14.trans age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(trans) post coeflegend
est store de_trans_inclgreens_all

restore

*Figure 1 incl. greens
coefplot (de_leftinclgreens_p_all, label(Parental class) msymbol(d) mcolor(black) ciopts(lcolor(black))) ///
(de_leftinclgreens_p_all, label(Respondent's class) msymbol(oh) mcolor(black) ciopts(lcolor(black))) ///
, bylabel("GERMANY") ///
	||, drop(_cons 5.simclass_r2 5.simclass_parent2)  ///
	xscale(lstyle(none)) yscale(lstyle(none)) xline(0) byopts(xrescale cols(3) /// 
	legend(position(6))) ///
coeflabels (1.simclass_r2 = "Service workers" ///
	2.simclass_r2 = "Production workers" ///
	3.simclass_r2 = "SCP" ///
	, wrap (10) notick labgap(1)) ///
	rename(1.simclass_parent2 = 1.simclass_r2 ///
2.simclass_parent2 = 2.simclass_r2 3.simclass_parent2 = 3.simclass_r2) ///
order(2.simclass_r2 1.simclass_r2 3.simclass_r2) ///
plotregion(lcolor(gs10) lwidth(small)) title (GERMANY) subtitle(Left/Green support)
graph save "${output}/Fig1_DE_inclgreens", replace
graph export "${output}/Fig1_DE_inclgreens.jpg", as(jpg) quality(100) replace


*Figure 2 incl. greens


coefplot (de_leftinclgreens_p_gen_3, label(Baby boomers)) (de_leftinclgreens_p_gen_4, label(Gen X)) (de_leftinclgreens_p_gen_5, label(Millennials)), bylabel("GERMANY" "Parental class") ///
	||, drop(_cons 5.simclass_r2 5.simclass_parent2) ///
	xscale(lstyle(none)) yscale(lstyle(none)) legend(cols(1))  byopts(xrescale cols(3) legend(position(6))) ///
coeflabels (1.simclass_r2 = "Service workers" ///
	2.simclass_r2 = "Production workers" ///
	3.simclass_r2 = "SCP" ///
	, wrap(10) notick labgap(1)) ///
	rename(1.simclass_parent2 = 1.simclass_r2 ///
2.simclass_parent2 = 2.simclass_r2 3.simclass_parent2 = 3.simclass_r2) ///
order(2.simclass_r2 1.simclass_r2 3.simclass_r2) ///
plotregion(lcolor(gs10) lwidth(small)) xline(0) title (GERMANY - PARENTAL CLASS) subtitle (Left/Green support)
graph save "${output}/Fig2_DE_inclgreens", replace
graph export "${output}/Fig2_DE_inclgreens.jpg", as(jpg) quality(100) replace


*Figure 3 incl. greens
coefplot (de_trans_inclgreens_all, label (All generations)), bylabel(GERMANY) ///
msymbol(o) mcolor(black) ciopts(lcolor(black)) ///
drop (2.trans* 4.trans* 7.trans* 8.trans* 10.trans* 12.trans* 20.trans* 21.trans* 22.trans* 23.trans* 24.trans* 25.trans* 26.trans* 27.trans* 28.trans* ) ///
xscale(lstyle(none)) yscale(lstyle(none)) legend(cols(4))  byopts(xrescale cols(3) legend(position(6))) ///
coeflabels(1.trans* = "Immobile prod. wrkrs" ///
			3.trans* = "Prod. wrkrs –> Serv. wrkrs" ///
			5.trans* = "Immobile SCP" ///
			6.trans* = "Prod. wrkrs –> SCP" ///
			9.trans* = "Prod. wrkrs –> OMC" ///
			11.trans* = "SCP –> OMC" ///
			13.trans* = "OMC –> SCP") ///
			headings ///
			(1.trans*= "{bf:Working class origins}" ///
			5.trans*= "{bf:Middle class origins}") ///
order(1.trans* 3.trans* 6.trans* 9.trans* 5.trans* 11.trans* 13.trans*) ///
			xscale(lstyle(none)) yscale(lstyle(none)) xline(0) ///
			plotregion(lcolor(gs10) lwidth(small)) title (GERMANY) subtitle(Left/Green support)
			graph save "${output}/Fig3_DE_inclgreens_pooled", replace
graph export "${output}/Fig3_DE_inclgreens_pooled.jpg", as(jpg) quality(100) replace

*****extra analyses

*class-party alignments over time 

preserve 
keep if syear==1990|syear==1991|syear==1992|syear==1993|syear==1994|syear==1995|syear==1996|syear==1997|syear==1998|syear==1999

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(simclass_r2) post coeflegend
est store de_left_r_all_90s
restore

preserve 
keep if syear==2000|syear==2001|syear==2002|syear==2003|syear==2004|syear==2005|syear==2006|syear==2007

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(simclass_r2) post coeflegend
est store de_left_r_all_00s
restore

preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989, re
margins, dydx(simclass_r2) post coeflegend
est store de_left_r_all_10s
restore



**LW support by parental and respondent's class, RE, excl. silent generation & time period 2009-2018

preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989, re
//our model: overall R-squared of 0.0150, between  0.0159

xtreg left i.egp_r i.egp_parent age female parttime ib3.civsta_3 i.loc1989, re
//with EGP-5 categories: overall R-squared of 0.0240, between 0.0239

xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3 i.loc1989, re
// our interaction model: overall R-squared of 0.019

xtreg left i.egp_r i.egp_parent##ib3.generations2 age female parttime ib3.civsta_3 i.loc1989, re
// interaction with EGP: overalll r-squared of 0.0268

xtreg left i.pgegp88 i.fegp88 age female parttime ib3.civsta_3 i.loc1989, re
//with full EGP scheme: overall R-squared of 0.0272, between 0.0266

xtreg left ib4.simclass_r2 i.egp_parent age female parttime ib3.civsta_3 i.loc1989, re
// combination of Oesch for respondents and EGP for parental class: overall R-squared of 0.0180, between 0.0188

restore

// control for income and/or education
preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

**model 1 - all generations
xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989, re
xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989 i.educat_3, re
xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.loc1989 i.educat_3 lnincome, re
// controlling for these factors does not make a big difference. after inclusion of education the size of some coefficients go down slightly, but patterns remain the same


**model 2 transitions - all generations
xtreg left ib14.trans age female parttime ib3.civsta_3 i.loc1989, re
xtreg left ib14.trans age female parttime ib3.civsta_3 i.loc1989 i.educat_3, re
xtreg left ib14.trans age female parttime ib3.civsta_3 i.loc1989 i.educat_3 lnincome, re
// not sure this comparison even makes sense because mobility is often achieved by education, but idem with respect to changes to size of coefficients / patterns of differences
restore

preserve 
keep if syear==2018|syear==2017|syear==2016|syear==2015|syear==2014|syear==2013|syear==2012|syear==2011|syear==2010|syear==2009|syear==2008

**control for income and education + EGP
xtreg left i.egp_r i.egp_parent age female parttime ib3.civsta_3 i.loc1989 , re

xtreg left i.egp_r i.egp_parent age female parttime ib3.civsta_3 i.loc1989 i.educat_3 lnincome, re


restore




********************************************************************************
*ANALYSIS SHP

use "${SHP}/SHP_workfile.dta"


numlabel, add
set scheme cleanplots

* Set the variables that order the panel
sort idpers year
xtset idpers year

* Define analytic sample: The analytic samples comprise all British respectively German, Swiss nationals aged over 17 years (eligible to vote), who were at the time of the survey employed and not enrolled in fulltime education.

* Generate the analytical sample based on these traits & key variables not missing, once we do this we select one observation per individual
capture drop sample
gen sample=0
replace sample=1 if age>17 & occupa<6 & educat_3!=0 & swiss==1 & class8_parent!=. & class8_r!=. & SP2!=.
keep if sample==1

* Select one observation per respondent
* Generate an identifier for the first time an individual was included in the sample and one for the last time
sort idpers year
bysort idpers: gen total= _N
bysort idpers: gen num_wave= _n
gen last_obs= total==num_wave

by idpers, sort: gen nfirst= _n==1
count if nfirst
by idpers, sort: gen nlast= _n==_N
count if nlast
*we have 9,256 respondents in the dataset
*there are 51,677 person-years

tab nfirst nlast
tab nfirst if nfirst==1 & nlast==1
* of which 1845 are only observed once


*1. Table 1& 2: Cross tabulation: father's class x respondent' class
*using waves 2019-2008
preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2008

tabulate simclass_parent2 simclass_r2, col, if last_obs==1 

restore
 // N=6503 unique observations

*2. Figure 1 and 2: LW support by parental and respondent's class, RE, excl. silent generation & time period 2008-2019
preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2008

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig1_table.xls", append ctitle(CH) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o)) // between R-squared is  0.0474; overall is 0.0388 
margins, dydx(simclass_r2) post coeflegend
est store ch_left_r_all

quietly xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_parent2) post coeflegend
est store ch_left_p_all

xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig2_table.xls", append ctitle(CH) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
xttab generations2 if e(sample)
// between percentages: 44% Baby boomers, 36% Gen X, 20% Millennials

forvalues i=3(1)5 {
quietly xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_parent2) at(generations2==`i') post coeflegend
est store ch_left_p_gen_`i'
}

restore

*3. Figure 3 and 4:
preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2008

xtreg left ib14.trans age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig3_table.xls", append ctitle(CH) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o)) // between R-squared is 0.0516 ; overall is 0.0429
margins, dydx(trans) post coeflegend
est store ch_trans_all

xtreg left ib14.trans##ib3.generations2 age female parttime ib3.civsta_3, re
outreg2 using "${output}/Fig4_table.xls", append ctitle(CH) label addstat("var level 2", e(sigma_u), "var level 1", e(sigma_e), "ICC", e(rho), "R-squared between", e(r2_b), "R-squared overall", e(r2_o))
margins, dydx(1.trans 3.trans 6.trans 9.trans 13.trans ) at(generations2==3) post coeflegend
est store ch_trans_gen3

forvalues i=4/5 {
quietly xtreg left ib14.trans##ib3.generations2 age female parttime ib3.civsta_3, re
margins, dydx(1.trans 3.trans 5.trans 6.trans 9.trans 11.trans 13.trans) at(generations2==`i') post coeflegend
est store ch_trans_gen`i'
}

restore



*checks*

**LW support by parental and respondent's class, RE, excl. silent generation & time period 2009-2019

preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2009|year==2008

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
//our model: overall R-squared of 0.039, between 0.0498

xtreg left i.egp_r i.egp_parent age female parttime ib3.civsta_3, re
//with EGP-5 categories: overall R-squared of 0.0248, between 0.0336

xtreg left ib4.simclass_r2 ib4.simclass_parent2##ib3.generations2 age female parttime ib3.civsta_3, re
// our interaction model: overall R-squared of 0.0410

xtreg left i.egp_r i.egp_parent##ib3.generations2 age female parttime ib3.civsta_3, re
// interaction with EGP: overalll r-squared of 0.0263


xtreg left ib4.simclass_r2 i.egp_parent age female parttime ib3.civsta_3, re
// combination of Oesch for respondents and EGP for parental class: overall R-squared of 0.0403, between 0.0521

restore

// control for income and/or education
preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2009

**model 1 - all generations
xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.educat_3, re
xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3 i.educat_3 lnincome, re
// controlling for education, the negative effect of production workers is no longer stat. significant. 
// the large effect of SCP remains. the pattern remains the same when adding income as well


**model 2 transitions - all generations
xtreg left ib14.trans age female parttime ib3.civsta_3, re
xtreg left ib14.trans age female parttime ib3.civsta_3 i.educat_3, re
xtreg left ib14.trans age female parttime ib3.civsta_3 i.educat_3 lnincome, re
// not sure this comparison even makes sense because mobility is often achieved by education, but patterns &  stat. sign. of the most relevant coefficients remaind unchanged
restore

preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2009

**control for income and education + EGP: does EGP mainly capture income/education?
xtreg left i.egp_r i.egp_parent age female parttime ib3.civsta_3, re
*overall r-squared 0.02

xtreg left i.egp_r i.egp_parent age female parttime ib3.civsta_3 i.educat_3 lnincome, re
*overall r-squared 0.03 --> still lower than our basic model with only Oesch class scheme

restore


*extra analyses - dependent variable incl. the greens

*Figure 1 and Figure 3 models
preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2008

xtreg left_green ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_r2) post coeflegend
est store ch_leftinclgreens_r_all

quietly xtreg left_green ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_parent2) post coeflegend
est store ch_leftinclgreens_p_all

xtreg left_green ib14.trans age female parttime ib3.civsta_3, re
margins, dydx(trans) post coeflegend
est store ch_trans_inclgreens_all

restore

**** extra analyses

*class-party alignments over time

preserve 
keep if year==1999|year==2000|year==2001|year==2002|year==2003|year==2004|year==2005|year==2006|year==2007

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_r2) post coeflegend
est store ch_left_r_all_00s
restore

preserve 
keep if year==2019|year==2018|year==2017|year==2016|year==2015|year==2014|year==2013|year==2012|year==2011|year==2010|year==2009|year==2008

xtreg left ib4.simclass_r2 ib4.simclass_parent2 age female parttime ib3.civsta_3, re
margins, dydx(simclass_r2) post coeflegend
est store ch_left_r_all_10s

restore

*Figure 1 incl. greens
coefplot (ch_leftinclgreens_r_all, label(All generations)), bylabel("SWITZERLAND" "Respondent's class") ///
msymbol(o) mcolor(black) ciopts(lcolor(black)) ///
	||(ch_leftinclgreens_p_all), bylabel("SWITZERLAND" "Father's class") ///
	||, drop(_cons 5.simclass_r2 5.simclass_parent2)  ///
	xscale(lstyle(none)) yscale(lstyle(none)) xline(0) byopts(xrescale cols(3) /// 
	legend(position(6))) ///
coeflabels (1.simclass_r2 = "Service workers" ///
	2.simclass_r2 = "Production workers" ///
	3.simclass_r2 = "SCP" ///
	, notick labgap(1)) ///
	rename(1.simclass_parent2 = 1.simclass_r2 ///
2.simclass_parent2 = 2.simclass_r2 3.simclass_parent2 = 3.simclass_r2) ///
order(2.simclass_r2 1.simclass_r2 3.simclass_r2 ) ///
plotregion(lcolor(gs10) lwidth(small))
graph save "${output}/Fig1_CH_inclgreens", replace
graph export "${output}/Fig1_CH_inclgreens.jpg", as(jpg) quality(100) replace

*Figure 3 incl. greens
coefplot (ch_trans_inclgreens_all, label (All generations)), bylabel(SWITZERLAND) ///
msymbol(o) mcolor(black) ciopts(lcolor(black)) ///
drop (2.trans* 4.trans* 7.trans* 8.trans* 10.trans* 12.trans* 20.trans* 21.trans* 22.trans* 23.trans* 24.trans* 25.trans* 26.trans* 27.trans* 28.trans* ) ///
xscale(lstyle(none)) yscale(lstyle(none)) legend(cols(4))  byopts(xrescale cols(3) legend(position(6))) ///
coeflabels(1.trans* = "Immobile prod. wrkrs" ///
			3.trans* = "Prod. wrkrs –> Serv. wrkrs" ///
			5.trans* = "Immobile SCP" ///
			6.trans* = "Prod. wrkrs –> SCP" ///
			9.trans* = "Prod. wrkrs –> OMC" ///
			11.trans* = "SCP –> OMC" ///
			13.trans* = "OMC –> SCP") ///
			headings ///
			(1.trans*= "{bf:Working class origins}" ///
			5.trans*= "{bf:Middle class origins}") ///
order(1.trans* 3.trans* 6.trans* 9.trans* 5.trans* 11.trans* 13.trans*) ///
			xscale(lstyle(none)) yscale(lstyle(none)) xline(0) ///
			plotregion(lcolor(gs10) lwidth(small)) 
			graph save "${output}/Fig3_CH_inclgreens_pooled", replace
graph export "${output}/Fig3_CH_inclgreens_pooled.jpg", as(jpg) quality(100) replace















*Combined graphs

*Fig. 1 

*code for plot across 3 countries, last 10 waves, pooled generations:
*both coefficients in the same graph 
coefplot (uk_left_p_all, label(Parental class) msymbol(d) mcolor(black) ciopts(lcolor(black))) ///
(uk_left_r_all, label(Respondent's class) msymbol(oh) mcolor(black) ciopts(lcolor(black))),  ///
bylabel("UNITED KINGDOM")  ///
	|| (de_left_p_all) (de_left_r_all), bylabel("GERMANY") ///
	|| (ch_left_p_all) (ch_left_r_all), bylabel("SWITZERLAND") ///
	||, drop(_cons 5.simclass_r2 5.simclass_parent2) ///
	xscale(lstyle(none)) yscale(lstyle(none)) byopts(cols(3) legend(position(6))) ///
coeflabels (1.simclass_r2 = "Service workers" ///
	2.simclass_r2 = "Production workers" ///
	3.simclass_r2 = "SCP" ///
	, wrap(10) notick labgap(1)) ///
	rename(1.simclass_parent2 = 1.simclass_r2 ///
2.simclass_parent2 = 2.simclass_r2 3.simclass_parent2 = 3.simclass_r2) ///
order(2.simclass_r2 1.simclass_r2 3.simclass_r2) ///
plotregion(lcolor(gs10) lwidth(small)) xline(0)
graph save "${output}/Fig1_2008_2019_new", replace
graph export "${output}/Fig1_2008_2019_new.jpg", as(jpg) quality(100) replace

*Fig. 2
*code for plot across 3 countries, last 10 waves, by generations:
coefplot (uk_left_p_gen_3, label(Baby boomers)) (uk_left_p_gen_4, label(Gen X)) (uk_left_p_gen_5, label(Millennials)), bylabel("UNITED KINGDOM" "Parental class") ///
	|| (de_left_p_gen_3) (de_left_p_gen_4) (de_left_p_gen_5), bylabel("GERMANY" "Parental class") ///
	|| (ch_left_p_gen_3) (ch_left_p_gen_4) (ch_left_p_gen_5), bylabel("SWITZERLAND" "Parental class") ///
	||, drop(_cons 5.simclass_r2 5.simclass_parent2) ///
	xscale(lstyle(none)) yscale(lstyle(none)) legend(cols(1))  byopts(xrescale cols(3) legend(position(6))) ///
coeflabels (1.simclass_r2 = "Service workers" ///
	2.simclass_r2 = "Production workers" ///
	3.simclass_r2 = "SCP" ///
	, wrap(10) notick labgap(1)) ///
	rename(1.simclass_parent2 = 1.simclass_r2 ///
2.simclass_parent2 = 2.simclass_r2 3.simclass_parent2 = 3.simclass_r2) ///
order(2.simclass_r2 1.simclass_r2 3.simclass_r2) ///
plotregion(lcolor(gs10) lwidth(small)) xline(0)
graph save "${output}/Fig2_2008_2019_new", replace
graph export "${output}/Fig2_2008_2019_new.jpg", as(jpg) quality(100) replace


*Fig. 3

*1, 3, 5, 6, 9, 11, 13 = of interest
*2, 4, 7, 8, 10, 12, 20-28 = skip

*pooled generations only:
coefplot (uk_trans_all, label (All generations)), bylabel(UNITED KINGDOM) ///
msymbol(o) mcolor(black) ciopts(lcolor(black)) ///
	|| (de_trans_all), bylabel(GERMANY) ///
	|| (ch_trans_all), bylabel(SWITZERLAND) ///
drop (2.trans* 4.trans* 7.trans* 8.trans* 10.trans* 12.trans* 20.trans* 21.trans* 22.trans* 23.trans* 24.trans* 25.trans* 26.trans* 27.trans* 28.trans* ) ///
xscale(lstyle(none)) yscale(lstyle(none)) legend(cols(4))  byopts(xrescale cols(3) legend(position(6))) ///
coeflabels(1.trans* = "Immobile prod. wrkrs" ///
			3.trans* = "Prod. wrkrs –> Serv. wrkrs" ///
			5.trans* = "Immobile SCP" ///
			6.trans* = "Prod. wrkrs –> SCP" ///
			9.trans* = "Prod. wrkrs –> OMC" ///
			11.trans* = "SCP –> OMC" ///
			13.trans* = "OMC –> SCP") ///
			headings ///
			(1.trans*= "{bf:Working class origins}" ///
			5.trans*= "{bf:Middle class origins}") ///
order(1.trans* 3.trans* 6.trans* 9.trans* 5.trans* 11.trans* 13.trans*) ///
			xscale(lstyle(none)) yscale(lstyle(none)) xline(0) ///
			plotregion(lcolor(gs10) lwidth(small)) 
			graph save "${output}/Fig3_pooled", replace
graph export "${output}/Fig3_pooled.jpg", as(jpg) quality(100) replace

*Fig. 4
*by generations

coefplot (uk_trans_gen3, label (Baby boomers)) /// 
(uk_trans_gen4, label (GenX)) (uk_trans_gen5, label (Millennials)), bylabel(UNITED KINGDOM) ///
	|| (de_trans_gen3) (de_trans_gen4) (de_trans_gen5), bylabel(GERMANY) ///
	|| (ch_trans_gen3) (ch_trans_gen4) (ch_trans_gen5), bylabel(SWITZERLAND) ///
drop (2.trans* 4.trans* 7.trans* 8.trans* 10.trans* 12.trans* 20.trans* 21.trans* 22.trans* 23.trans* 24.trans* 25.trans* 26.trans* 27.trans* 28.trans* ) ///
xscale(lstyle(none)) yscale(lstyle(none)) legend(cols(4))  byopts(xrescale cols(3) legend(position(6))) ///
coeflabels(1.trans* = "Immobile prod. wrkrs" ///
			3.trans* = "Prod. wrkrs –> Serv. wrkrs" ///
			5.trans* = "Immobile SCP" ///
			6.trans* = "Prod. wrkrs –> SCP" ///
			9.trans* = "Prod. wrkrs –> OMC" ///
			11.trans* = "SCP –> OMC" ///
			13.trans* = "OMC –> SCP") ///
			headings ///
			(1.trans*= "{bf:Working class origins}" ///
			5.trans*= "{bf:Middle class origins}") ///
order(1.trans* 3.trans* 6.trans* 9.trans* 5.trans* 11.trans* 13.trans*) ///
			xscale(lstyle(none)) yscale(lstyle(none)) xline(0) ///
			plotregion(lcolor(gs10) lwidth(small)) 
graph save "${output}/Fig4_3gen", replace
graph export "${output}/Fig4_3gen.jpg", as(jpg) quality(100) replace


*extra figures: 


*all respondents, by time periods - together in 1 subgraph per country
coefplot (uk_left_r_all_10s, label(2008-2018/9) msymbol(dh) mcolor(pink) ciopts(lcolor(pink))) ///
(uk_left_r_all_00s, label(2000-2007) msymbol(s) mcolor(ltblue) ciopts(lcolor(ltblue))) ///
(uk_left_r_all_90s, label(1990s) msymbol(t) mcolor(navy) ciopts(lcolor(navy))) ///
, bylabel("UNITED KINGDOM" "Respondent's class") ///
	|| (de_left_r_all_10s) (de_left_r_all_00s) (de_left_r_all_90s), bylabel("GERMANY" "Respondent's class") ///
	|| (ch_left_r_all_10s) (ch_left_r_all_00s), bylabel("SWITZERLAND" "Respondent's class") ///
	||, drop(_cons 5.simclass_r2 5.simclass_parent2) ///
	xscale(lstyle(none)) yscale(lstyle(none)) legend(cols(1)) byopts(cols(3) legend(position(6))) ///
coeflabels (1.simclass_r2 = "Service workers" ///
	2.simclass_r2 = "Production workers" ///
	3.simclass_r2 = "SCP" ///
	, wrap(10) notick labgap(1)) ///
	rename(1.simclass_parent2 = 1.simclass_r2 ///
2.simclass_parent2 = 2.simclass_r2 3.simclass_parent2 = 3.simclass_r2) ///
order(2.simclass_r2 1.simclass_r2 3.simclass_r2) ///
plotregion(lcolor(gs10) lwidth(small)) xline(0)
graph save "${output}/Fig1b_periods", replace
graph export "${output}/Fig1b_periods.jpg", as(jpg) quality(100) replace


